<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/h_something/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/h_something/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/h_something/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/h_something/images/logo.svg" color="#222">

<link rel="stylesheet" href="/h_something/css/main.css">


<link rel="stylesheet" href="/h_something/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"ddota2.gitee.io","root":"/h_something/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="记录一次使用关于通过netcat工具连接socket的问题吧环境本地环境 win10 企业版（2019-1206安装，奇怪不应该是专业版吗，而且我没重装系统呀，难道是上次注册表的问题？）:  netcat: v1.12 NT jdk: 1.8.0_112 正式环境 ubuntu: 16.04.6 netcat: OpenBSD netcat (Debian patchlevel 1.105-7ub">
<meta property="og:type" content="article">
<meta property="og:title" content="socket-readLine-低级错误">
<meta property="og:url" content="https://ddota2.gitee.io/2020/01/02/socket-readLine-%E4%BD%8E%E7%BA%A7%E9%94%99%E8%AF%AF/index.html">
<meta property="og:site_name" content="D">
<meta property="og:description" content="记录一次使用关于通过netcat工具连接socket的问题吧环境本地环境 win10 企业版（2019-1206安装，奇怪不应该是专业版吗，而且我没重装系统呀，难道是上次注册表的问题？）:  netcat: v1.12 NT jdk: 1.8.0_112 正式环境 ubuntu: 16.04.6 netcat: OpenBSD netcat (Debian patchlevel 1.105-7ub">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-01-02T08:11:09.000Z">
<meta property="article:modified_time" content="2020-11-19T08:41:30.573Z">
<meta property="article:author" content="Dota2 player">
<meta property="article:tag" content="java">
<meta property="article:tag" content="socket">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://ddota2.gitee.io/2020/01/02/socket-readLine-%E4%BD%8E%E7%BA%A7%E9%94%99%E8%AF%AF/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>socket-readLine-低级错误 | D</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/h_something/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">D</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">而已</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/h_something/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/h_something/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/h_something/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/h_something/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://ddota2.gitee.io/2020/01/02/socket-readLine-%E4%BD%8E%E7%BA%A7%E9%94%99%E8%AF%AF/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/h_something/images/avatar.gif">
      <meta itemprop="name" content="Dota2 player">
      <meta itemprop="description" content="楼下的男人病的要死">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="D">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          socket-readLine-低级错误
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-01-02 16:11:09" itemprop="dateCreated datePublished" datetime="2020-01-02T16:11:09+08:00">2020-01-02</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-11-19 16:41:30" itemprop="dateModified" datetime="2020-11-19T16:41:30+08:00">2020-11-19</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/h_something/categories/java/" itemprop="url" rel="index"><span itemprop="name">java</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="记录一次使用关于通过netcat工具连接socket的问题吧"><a href="#记录一次使用关于通过netcat工具连接socket的问题吧" class="headerlink" title="记录一次使用关于通过netcat工具连接socket的问题吧"></a>记录一次使用关于通过netcat工具连接socket的问题吧</h1><h2 id="环境"><a href="#环境" class="headerlink" title="环境"></a>环境</h2><pre><code>本地环境
win10 企业版（2019-1206安装，奇怪不应该是专业版吗，而且我没重装系统呀，难道是上次注册表的问题？）: 
netcat: v1.12 NT
jdk: 1.8.0_112
正式环境
ubuntu: 16.04.6
netcat: OpenBSD netcat (Debian patchlevel 1.105-7ubuntu1)
jdk: 1.8.0_201-b09 </code></pre><p>在写连接设备程序时，用到了</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">String sb = bufferedReader.readLine()</span><br><span class="line">...</span><br></pre></td></tr></table></figure>
<p>程序直接部署上去，结果一夜过后发现几个T的容量直接沾满了，导致数据库挂掉了。于是查看一下是因为此程序的log写满了，觉着奇怪。删掉日志文件，重新启动，日志也没有飞速增加。<br>但是，当我使用ubuntu的nc工具连接然后断开时，发现几乎以满IO的速度写入日志。发现 bufferedReader.readLine() 不断的获取到null，然后空指针异常填满了日志。于是程序改成了</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span> ((line = bufferedReader.readLine()) != <span class="keyword">null</span>) &#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这样改了之后，日志确实不会继续增加了，但是发现一个新的问题，每断开一个连接，cpu就会占满一个核，使用top命令发现%CPU指数基本为（断开次数*100%），于是发现是 nc 工具ctrl+c后不会关闭socket的问题。<br>因为我外部还套了一层循环，但是在windows使用nc测试时，如果断开连接则会抛出异常Connection reset，从而跳出循环。也就不存在什么资源占用的问题。但是ubuntu的nc命令，断开连接后，socket 仍然未关闭，可以不断的重新连接，然后while true readline。<br>查看了外套的循环的作用，发现基本是while true，可能是怕读取到空值后，断开连接，于是加了层循环，若读取到null后，重新获取inputstream继续readline，找了相关资料，socket发送null，基本就是等于断开连接了。于是删除此外套的循环，问题解决。</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/h_something/tags/java/" rel="tag"># java</a>
              <a href="/h_something/tags/socket/" rel="tag"># socket</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/h_something/2019/12/06/windows-problem-1/" rel="prev" title="windows_problem_1">
      <i class="fa fa-chevron-left"></i> windows_problem_1
    </a></div>
      <div class="post-nav-item">
    <a href="/h_something/2020/02/11/pg-pool/" rel="next" title="pg_pool 简单教程">
      pg_pool 简单教程 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#记录一次使用关于通过netcat工具连接socket的问题吧"><span class="nav-number">1.</span> <span class="nav-text">记录一次使用关于通过netcat工具连接socket的问题吧</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#环境"><span class="nav-number">1.1.</span> <span class="nav-text">环境</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Dota2 player</p>
  <div class="site-description" itemprop="description">楼下的男人病的要死</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/h_something/archives/">
        
          <span class="site-state-item-count">11</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/h_something/categories/">
          
        <span class="site-state-item-count">5</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/h_something/tags/">
          
        <span class="site-state-item-count">15</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Dota2 player</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/h_something/lib/anime.min.js"></script>
  <script src="/h_something/lib/velocity/velocity.min.js"></script>
  <script src="/h_something/lib/velocity/velocity.ui.min.js"></script>

<script src="/h_something/js/utils.js"></script>

<script src="/h_something/js/motion.js"></script>


<script src="/h_something/js/schemes/muse.js"></script>


<script src="/h_something/js/next-boot.js"></script>




  















  

  

</body>
</html>
